#include "mainscene.h"
#include<config.h>
#include<QIcon>
#include<QPainter>
#include<QMouseEvent>
#include<ctime>
#include <QSoundEffect>
#include<over.h>

Mainscene::Mainscene(QWidget *parent)
    : QMainWindow(parent)
{
    //调用初始化场景
    initScene();
    //启动游戏
    playGame();
}

Mainscene::~Mainscene()
{

}

void Mainscene::initScene()
{
    //设置窗口固定尺寸
    setFixedSize(GAME_WIDTH,GAME_HEIGHT);
    //设置标题
    setWindowTitle(GAME_TITLE);
    //加载图标
    setWindowIcon(QIcon(GAME_ICON));
    //定时器
    m_Timer.setInterval(GAME_RATE);
    //蛋仔出场时间间隔初始化
    m_recorder=0;
    //随机数种子
    srand((unsigned int)time(NULL));  
}

void Mainscene::playGame()
{
    music->setSource(QUrl::fromLocalFile(SOUND_BACK));
    music->setLoopCount(QSoundEffect::Infinite);
    music->setVolume(0.2f);
    music->play();
    m_Timer.start();
    connect(&m_Timer,&QTimer::timeout,[=](){
        danzaicoming();
        updatePosition();
        update();
        collisionDetection();        
    });
}

void Mainscene::updatePosition()
{
    //更新地图坐标
    m_map.mapPosition();
    //发射子弹
    m_slwp.shoot();
    //计算所有非空闲子弹的当前坐标
    for(int i=0;i<BULLET_NUM;i++)
    {
        if(m_slwp.m_bullets[i].m_Free==false)
        {
            m_slwp.m_bullets[i].updatePosition();
        }
    }
    //蛋仔出场
    for(int i=0;i<DANZAI_NUM;i++)
    {
        if(m_danzai[i].m_Free==false)
        {
            m_danzai[i].updatePosition();
        }
    }
    //计算爆炸的播放的图片
    for(int i=0;i<BOMB_NUM;i++)
    {
        if(m_bomb[i].m_Free==false)
        {
            m_bomb[i].updateInfo();
        }
    }
}

void Mainscene::paintEvent(QPaintEvent *)
{
    QPainter painter(this);
    //绘制地图
    painter.drawPixmap(m_map.m_map1_posX,m_map.m_map1_posY,m_map.m_map1);
    painter.drawPixmap(m_map.m_map2_posX,m_map.m_map2_posY,m_map.m_map2);
    //绘制鲨莉娃
    painter.drawPixmap(m_slwp.m_X,m_slwp.m_Y,m_slwp.m_slwp);
    //绘制子弹
    for(int i=0;i<BULLET_NUM;i++)
    {
        if(m_slwp.m_bullets[i].m_Free==false)
        {
            painter.drawPixmap(m_slwp.m_bullets[i].m_X,m_slwp.m_bullets[i].m_Y,m_slwp.m_bullets[i].m_Bullet);
        }
    }
    //绘制蛋仔
    for(int i=0;i<DANZAI_NUM;i++)
    {
        if(m_danzai[i].m_Free==false)
        {
            painter.drawPixmap(m_danzai[i].m_X,m_danzai[i].m_Y,m_danzai[i].m_danzai);
        }
    }
    //绘制爆炸
    for(int i=0;i<BOMB_NUM;i++)
    {
        if(m_bomb[i].m_Free==false)
        {
            painter.drawPixmap(m_bomb[i].m_X,m_bomb[i].m_Y,m_bomb[i].m_pixArr[m_bomb[i].m_index]);
        }
    }
    //显示得分
    QFont font("宋体",30,QFont::Bold);
    painter.setFont(font);
    painter.setPen(Qt::white);
    painter.drawText(100,50,"SCORE: ");
    painter.drawText(250,50,QString::number(Score));
}

void Mainscene::mouseMoveEvent(QMouseEvent *event)
{
    int x=event->x()-m_slwp.m_Rect.width()*0.5;
    int y=event->y()-m_slwp.m_Rect.height()*0.5;
    //边界检测
    if(x<=0)
    {
        x=0;
    }
    if(x>=GAME_WIDTH-m_slwp.m_Rect.width())
    {
        x=GAME_WIDTH-m_slwp.m_Rect.width();
    }
    if(y<=0)
    {
        y=0;
    }
    if(y>=GAME_HEIGHT-m_slwp.m_Rect.height())
    {
        y=GAME_HEIGHT-m_slwp.m_Rect.height();
    }
    m_slwp.setPosition(x,y);
}

void Mainscene::danzaicoming()
{
    m_recorder++;
    //未达到间隔就不出场
    if(m_recorder<DANZAI_INTERVAL)
    {
        return;
    }
    //达到间隔
    m_recorder=0;
    for(int i=0;i<DANZAI_NUM;i++)
    {
        //如果空闲 出场
        if(m_danzai[i].m_Free)
        {
            m_danzai[i].m_Free=false;
            m_danzai[i].m_X=GAME_WIDTH;
            m_danzai[i].m_Y=rand()%(GAME_HEIGHT-m_danzai[i].m_Rect.height());
            break;
        }
    }   
}

void Mainscene::collisionDetection()
{
    bool gameOver = false;
    for(int i=0;i<DANZAI_NUM;i++)
    {
        if(m_danzai[i].m_Free)
        {
            continue;
        }
        for(int j=0;j<BULLET_NUM;j++)
        {
            if(m_slwp.m_bullets[j].m_Free)
            {
                continue;
            }
            //如果打中蛋仔 将子弹和蛋仔的空闲状态时设为真
            if(m_danzai[i].m_Rect.intersects(m_slwp.m_bullets[j].m_Rect))
            {
                m_danzai[i].m_Free=true;
                m_slwp.m_bullets[j].m_Free=true;
                //击中一次蛋仔加10分
                Score+=10;
                //达到200分 游戏结束
                if(Score >= 200)
                {
                    gameOver = true;
                    break; // 跳出内层循环
                }
                //爆炸效果
                for(int k=0;k<BULLET_NUM;k++)
                {
                    if (m_bomb[k].m_Free)
                    {
                        QSoundEffect *music1 = new QSoundEffect();
                        music1->setSource(QUrl::fromLocalFile(SOUND_BOMB));
                        music1->setVolume(0.8f);
                        music1->play();
                        m_bomb[k].m_Free=false;
                        m_bomb[k].m_X=m_danzai[i].m_X;
                        m_bomb[k].m_Y=m_danzai[i].m_Y;
                        break;
                    }                    
                }
            }
        }
        if(gameOver) break;
    }
    if(gameOver)
    {
        m_Timer.stop();
        music->stop();
        //结束界面 音效
        over* ui = new over();
        ui->show();
        QSoundEffect* winSound = new QSoundEffect();
        winSound->setSource(QUrl::fromLocalFile(SOUND_WIN));
        winSound->setVolume(1.0f);
        winSound->play();
    }
}
